package com.koron.plan.web.statistic.util;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 获取上个月的第一天：2024-03-01 00:00:00
     * @return
     */
    public static String getFirstDayOfMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, LocalDate.now().getYear());
        calendar.set(Calendar.MONTH, Calendar.getInstance().get(Calendar.MONTH)-1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        String format = cn.hutool.core.date.DateUtil.format(time, "yyyy-MM-dd HH:mm:ss");
        return format;
    }

    /**
     * 获取这个月的第一天：2024-04-01 00:00:00
     * @return
     */
    public static String getLastDayOfMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, LocalDate.now().getYear());
        calendar.set(Calendar.MONTH, Calendar.getInstance().get(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().getMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        String format = cn.hutool.core.date.DateUtil.format(time, "yyyy-MM-dd HH:mm:ss");
        return format;
    }

    public static String getMonth(){
        // 获取上个月
        String month = LocalDate.now().minus(Period.ofMonths(1)).format(DateTimeFormatter.ofPattern("yyyy-MM")).toString();
        return month;
    }

    public static String getYear(){
        // 获取去年
        String year = LocalDate.now().minus(Period.ofYears(1)).format(DateTimeFormatter.ofPattern("yyyy")).toString();
        return year;
    }


    /**
     * 获取去年：2023-01-01 00:00:00
     * @return
     */
    public static String getLastYear(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR) - 1);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        String format = cn.hutool.core.date.DateUtil.format(time, "yyyy-MM-dd HH:mm:ss");
        return format;
    }

    /**
     * 获取今年：2024-01-01 00:00:00
     * @return
     */
    public static String getCurYear(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        String format = cn.hutool.core.date.DateUtil.format(time, "yyyy-MM-dd HH:mm:ss");
        return format;
    }
}
